package com.joeyzh.utils;

import android.os.Environment;
import android.text.TextUtils;

import com.joey.base.util.SharedPreferenceUtils;
import com.joeyzh.BuildConfig;

import java.io.File;

/**
 * Created by Joey on 2018/5/7.
 * 使用到的常量部分的定义
 *
 * @author Joeyzh
 */

public class AppConsts {

    public final static String APP_NAME = BuildConfig.APP_NAME;
    /**
     * 标记是否为debug版本
     */
    public static boolean DEBUG = BuildConfig.DEBUG;

    /**
     * 文件路径常量类
     */
    public static final class Path {
        //SD卡根路径
        private static final String mSdRootPath = Environment.getExternalStorageDirectory().getPath();
        //手机的缓存根目录
        public static final String mDataRootPath = mSdRootPath + File.separator + APP_NAME;
        //保存图片的目录
        public final static String IMG_PATH = mDataRootPath + File.separator + "image";
        //下载
        public final static String DOWNLOAD_PATH = mDataRootPath + File.separator + "download";
        //缓存
        public final static String CACHE_PATH = mDataRootPath + File.separator + "cache";
        // 更新目录
        public final static String UPDATE_PATH = DOWNLOAD_PATH + File.separator + "update";

    }

    /**
     * 路径url常量类，url都在该类定义
     */
    public static final class Url {
        //       域名
        private final static String IP_ADDR = "172.16.5.249"; //"192.168.137.1";
        private final static String PORT = "80";//"2007";//"3306";
        private final static String PROG_NAME = "";
        private static String DOMAIN_ROOT = "http://" + IP_ADDR + ":" + PORT + "/" + PROG_NAME + "/";

        public static String setServer(String ip, String port, String progName) {
            String ip1 = IP_ADDR;
            String port1 = PORT;
            String progName1 = PROG_NAME;
            if (ip != null && !ip.isEmpty()) {
                ip1 = ip;
            }
            if (port != null && !port.isEmpty()) {
                port1 = port;
            }
            if (progName != null && !progName.isEmpty()) {
                progName1 = progName;
            }
            SharedPreferenceUtils.getInstance().putString("host", ip1);
            SharedPreferenceUtils.getInstance().putString("port", port1);
            SharedPreferenceUtils.getInstance().putString("server", progName1);
            String temp;
            if (TextUtils.isEmpty(port1)) {
                temp = String.format("http://%s/", ip1);
            } else {
                temp = String.format("http://%s:%s/", ip1, port1);
            }
            if (!TextUtils.isEmpty(progName1)) {
                DOMAIN_ROOT = String.format("%s%s/", temp, progName1);
            } else {
                DOMAIN_ROOT = temp;
            }

            return DOMAIN_ROOT;
        }

        public static String getDomainRoot() {
            return DOMAIN_ROOT;
        }

        public static String resetServer() {
            return setServer(IP_ADDR, PORT, PROG_NAME);
        }

    }

}
